package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.DeliveryaddressDao;
import com.foreknow.elm.po.Deliveryaddress;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DeliveryaddressDaoImpl implements DeliveryaddressDao {
    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;
    private int isRight = 0;

    @Override
    public List<Deliveryaddress> dlistDeliveryAddressByUserId(String userid) throws SQLException {
        List<Deliveryaddress> list = new ArrayList<>();
        String sql = "select * from deliveryaddress where userid=?";
        try{
        con = DBUtil.getConnection();
        pst = con.prepareStatement(sql);
        pst.setString(1, userid);
        rs = pst.executeQuery();
        while (rs.next()){
        Deliveryaddress deliveryaddress = new Deliveryaddress();
        deliveryaddress.setDaId(rs.getInt("daId"));
        deliveryaddress.setContactName(rs.getString("contactName"));
        deliveryaddress.setContactSex(rs.getInt("contactSex"));
        deliveryaddress.setContactTel(rs.getString("contactTel"));
        deliveryaddress.setAddress(rs.getString("address"));
        deliveryaddress.setUserId(rs.getString("userId"));
        list.add(deliveryaddress);
        }
        }finally {
            DBUtil.close(rs,pst);
        }
        return list;
    }

    @Override
    public Deliveryaddress dgetDeliveryAddressById(Integer daid) throws SQLException {
        Deliveryaddress deliveryaddress = new Deliveryaddress();
        String sql = "select * from deliveryaddress where daid=?";
        try{
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1, daid);
            rs = pst.executeQuery();
            if (rs.next()) {
                deliveryaddress.setDaId(rs.getInt("daId"));
                deliveryaddress.setContactName(rs.getString("contactName"));
                deliveryaddress.setContactSex(rs.getInt("contactSex"));
                deliveryaddress.setContactTel(rs.getString("contactTel"));
                deliveryaddress.setAddress(rs.getString("address"));
                deliveryaddress.setUserId(rs.getString("userId"));
            }
        } finally {
            DBUtil.close(rs,pst);
        }
        return deliveryaddress;
    }

    @Override
    public int dsaveDeliveryAddress(Deliveryaddress deliveryaddress) throws SQLException {
        String sql = "insert into deliveryaddress(contactName,contactSex,contactTel,address,userId) values(?,?,?,?,?)";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1,deliveryaddress.getContactName());
            pst.setInt(2,deliveryaddress.getContactSex());
            pst.setString(3,deliveryaddress.getContactTel());
            pst.setString(4,deliveryaddress.getAddress());
            pst.setString(5,deliveryaddress.getUserId());
            isRight = pst.executeUpdate();
        }finally {
            DBUtil.close(pst);
        }

        return isRight;
    }

    @Override
    public int dupdateDeliveryAddress(Deliveryaddress deliveryaddress) throws SQLException {
        String sql = "update deliveryaddress set contactName=?,contactSex=?,contactTel=?,address=?,userId=? where daid=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1,deliveryaddress.getContactName());
            pst.setInt(2,deliveryaddress.getContactSex());
            pst.setString(3,deliveryaddress.getContactTel());
            pst.setString(4,deliveryaddress.getAddress());
            pst.setString(5,deliveryaddress.getUserId());
            pst.setInt(6,deliveryaddress.getDaId());
            isRight = pst.executeUpdate();
        }finally {
            DBUtil.close(pst);
        }

        return isRight;
    }

    @Override
    public int dremoveDeliveryAddress(Integer daid) throws SQLException {
        String sql = "delete from deliveryaddress where daid=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1,daid);
            isRight = pst.executeUpdate();
        }finally {
            DBUtil.close(pst);
        }

        return isRight;
    }
}
